var express = require('express');
var router = express.Router();

const banner = require("../sql/banner");
const sql = require("../sql/sql");
const uuid = require("node-uuid");

/* GET home page. */
// 初始查询现有数据。渲染到页面模板
router.get('/', function(req, res, next) {
  sql.find(banner, {}, {}).then(data=>{
    res.render('banner', {
      index:4,
      data,
      power:req.session.power
    });
  })
});

// 删除路由
router.get('/del', function(req, res, next) {
  let {id} = req.query;
  sql.delete(banner, {imgId:id}).then(data=>{
    res.redirect("/banner")
  })
});

// 添加路由
router.post('/add', function(req, res, next) {
  // console.log(req.body);
  let {tit,alt,img} = req.body;

  sql.insert(banner, {
    imgId:"img"+uuid.v1(),
    title:tit,
    alt,
    img
  }).then(()=>{
    res.send("ok");
  })
});
module.exports = router;
